import '../../core/service_locator.dart';
import '../../models/product_model.dart';
import '../../core/http_client.dart';
import '../../models/product_detail_model.dart';

class ProductService {
  final HttpClient _http = ServiceLocator.httpClient;

  Future<List<Product>> getProducts({
    int page = 1,
    int limit = 10,
    String? categoryId,
  }) async {
    final response = await _http.get(
      '/products',
      params: {
        'page': page.toString(),
        'limit': limit.toString(),
        if (categoryId != null) 'category': categoryId,
      },
    );
    return (response.data as List).map((e) => Product.fromJson(e)).toList();
  }

  Future<Product> getProductDetail1(String productId) async {
    final response = await _http.get('/products/$productId');
    return Product.fromJson(response.data);
  }

  Future<List<Product>> searchProducts(String query) async {
    final response = await _http.get('/products/search', params: {'q': query});
    return (response.data as List).map((e) => Product.fromJson(e)).toList();
  }

  Future<ProductDetail> getProductDetail(String productId) async {
    final response = await _http.get('/products/$productId');
    return ProductDetail.fromJson(response.data);
  }
}
